home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / mmailp.idb / usr / lib / Zmail / bin / lookup.pw.z / lookup.pw
Encoding:
Text File  |  1997-01-22  |  2.3 KB  |  73 lines

  1. :
  2. #
  3. # E-Mail adddress directory lookup script to read the passwd file
  4. #
  5. if test $# -ne 2
  6. then
  7.     echo "`basename $0`: Expected 2 args, got $#"
  8.     exit 3                    # Incorrect arguments
  9. fi
  10. matches=${TMPDIR-/tmp}/pwmatch$$
  11. domainname=`(domainname) 2>/dev/null`
  12. if test -n "$domainname"
  13. then
  14.     domainname="-d $domainname"
  15. else
  16.     hostname=`( hostname || uname -n || uuname -l ) 2>/dev/null`
  17.     case "$hostname" in
  18.     *.*) domainname="-d $hostname";;
  19.     esac
  20. fi
  21.  
  22. # make SP = space + tab
  23. SP=`echo x | tr x '\011'``echo x | tr x '\040'`
  24.  
  25. # extract address part if this doesn't look like it's already a regex
  26. # this won't work absolutely 100% but it should get it right on
  27. # non-perverse cases
  28. addressonly=$2
  29. if echo "$addressonly" | grep -v '[]\*]' >/dev/null
  30. then
  31.     # the following sed expressions do this, in order:
  32.     # - assume that anything in double quotes is a comment and drop it
  33.     # - pull out the address from any remaining comments
  34.     # - squeeze adjacent whitespace, strip leading/trailing whitespace
  35.     # - drop anything that looks like a relay/host name; in particular
  36.     #   user@host, host!user@relay, user%host@relay, host1!host2!...!user
  37.     addressonly=`echo " $addressonly " \
  38.         | sed -e 's/\([^!]\)"[^"]*"\([^@]\)/\1\2/g' \
  39.               -e 's/([^)]*)//g' -e 's/^.*<\([^>]*\)>.*$/\1/' \
  40.               -e "s/[$SP][$SP]*/ /g" -e "s/^[$SP]//" -e "s/[$SP]$//" \
  41.               -e 's/@.*$//' -e 's/%.*$//' -e 's/^.*!\([^!]*\)$/\1/'`
  42.  
  43.     # if we somehow nuked the entire string, fall back on the original
  44.     # string, stripping one level of quoting if it exists
  45.     test -z "$addressonly" && addressonly=`echo "$2" \
  46.         | sed -e 's/^"\(.*\)"$/\1/'`
  47. fi
  48.  
  49. getpasswd="(ypcat $domainname passwd || cat /etc/passwd) 2>/dev/null"
  50. eval "$getpasswd" | cut -d: -f1,5 | grep "$addressonly" > $matches
  51. count=`wc -l < $matches`
  52. EXIT=1                        # Execution failure
  53. if test $count -eq 0
  54. then
  55.     echo "$2"
  56.     EXIT=4                    # No matches found
  57. elif test $count -gt $1 -a $1 -gt 0
  58. then
  59.     echo "Matched $count names (max $1)"
  60.     echo "Use a more specific pattern please."
  61.     EXIT=2                    # Too many matches
  62. else
  63.     sed 's/\([^:]*\):\([^,]*\),.*$/\1 (\2)/' < $matches
  64.     if test $count -eq 1
  65.     then
  66.     EXIT=5                    # Exactly one match
  67.     else
  68.     EXIT=0                    # At least one match
  69.     fi
  70. fi
  71. rm -f $matches
  72. exit $EXIT
  73.